package com.yimi.yinhepda.db.dao;

import com.j256.ormlite.stmt.DeleteBuilder;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;
import com.yimi.yinhepda.base.BaseApplication;
import com.yimi.yinhepda.db.helper.DbHelper;
import com.yimi.yinhepda.db.table.BuildPckScanTable;

import java.sql.SQLException;
import java.util.List;

/**
 * @author 000432
 * @date 2017/4/9 0009
 * <p>
 * 建包已扫描的数据
 */

public class BuildPckScanTableDao extends BaseDao<BuildPckScanTable, Long> {

    public BuildPckScanTableDao() {
        super(DbHelper.getInstance(BaseApplication.getContext()), BuildPckScanTable.class);
    }

    /**
     * 查询未上传的数量
     */
    public int queryNotUploadDataNum() {

        try {
            QueryBuilder<BuildPckScanTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("isUpload", 0);
            return (int) queryBuilder.countOf();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 查询未上传的数量
     */
    public int queryNotUploadDataNum(String taskNo) {

        try {
            QueryBuilder<BuildPckScanTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("isUpload", 0);
            where.and().eq("taskNo", taskNo);
            return (int) queryBuilder.countOf();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * @return
     */
    public int getScannedDataList(String waybillNo) {
        try {
            QueryBuilder<BuildPckScanTable, Long> queryBuilder = dao.queryBuilder();
            List<BuildPckScanTable> buildPckScanListData = queryBuilder.where().eq("waybillNo", waybillNo).query();
            return buildPckScanListData.size();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 根据任务号查询为上传的数据
     */
    public List<BuildPckScanTable> getUnUploadDataList(String taskNo) {
        try {
            QueryBuilder<BuildPckScanTable, Long> queryBuilder = dao.queryBuilder();
            /**
             * 查询字段“isUpload”等于0的条件数据，
             * 相当于sql语句"select * from 'buildPckScanTable' where 'isUpload' == 0 "
             *
             */
            List<BuildPckScanTable> buildPckScanListData = queryBuilder.where().eq("isUpload", 0).and().eq("taskNo", taskNo).query();

            return buildPckScanListData;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 更新表数据对象
     */
    public int updataItem(BuildPckScanTable itemTable) {
        int result = -1;
        try {
            result = this.update(itemTable);
        } catch (SQLException e) {
            e.printStackTrace();
            result = -1;
        }
        return result;
    }

    /**
     * 删除根据子单号
     */
    public int delelteByTaskNoAndChildBill(String taskNo, String childBill) {
        int result = -1;
        try {
            DeleteBuilder<BuildPckScanTable, Long> deleteBuilder = dao.deleteBuilder();
            deleteBuilder.where().eq("taskNo", taskNo).and().eq("childWaybillNo", childBill);
            result = deleteBuilder.delete();
        } catch (Exception e) {
            e.printStackTrace();
            result = -1;
        }
        return result;
    }

    /**
     * 根据任务号删除
     */
    public int delelteByTaskNo(String taskNo) {
        int result = -1;
        try {
            DeleteBuilder<BuildPckScanTable, Long> deleteBuilder = dao.deleteBuilder();
            deleteBuilder.where().eq("taskNo", taskNo);
            result = deleteBuilder.delete();
        } catch (Exception e) {
            e.printStackTrace();
            result = -1;
        }
        return result;
    }

    /**
     * 获取未完成提交的建包数据
     */
    public List<BuildPckScanTable> findUnComplete(int status, String userCode) {
        try {
            QueryBuilder<BuildPckScanTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("status", status).and().eq("operEmpCode", userCode);
            List<BuildPckScanTable> listUnloadScanTable = queryBuilder.query();
            return listUnloadScanTable;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据任务号删除数据
     *
     * @param taskNo 任务号
     */
    public void deleteDataByTaskNo(String taskNo) {
        try {
            DeleteBuilder<BuildPckScanTable, Long> deleteBuilder = dao.deleteBuilder();
            deleteBuilder.where().eq("taskNo", taskNo);
            deleteBuilder.delete();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 查询本地数据库中是否存在暂存的数据
     */
    public boolean existDataByTaskNoAndWaybillNo(String taskNo, String childWaybillNo) {
        try {
            QueryBuilder<BuildPckScanTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("taskNo", taskNo).and().eq("childWaybillNo", childWaybillNo);
            BuildPckScanTable bean = queryBuilder.queryForFirst();
            if (bean != null) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 根据任务号和主单号查询数据
     *
     * @param taskNo    任务号
     * @param waybillNo 主单号
     */
    public List<BuildPckScanTable> getDataByTaskNoAndWayBillNo(String taskNo, String waybillNo) {
        try {
            QueryBuilder<BuildPckScanTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("taskNo", taskNo).and().eq("waybillNo", waybillNo);
            return queryBuilder.query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据任务号和强扫标记查询数据
     *
     * @param taskNo 任务号
     * @param remark 强扫标记
     */
    public List<BuildPckScanTable> getDataByTaskNoAndRemark(String taskNo, int remark) {
        try {
            QueryBuilder<BuildPckScanTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("taskNo", taskNo).and().eq("remark", remark);
            return queryBuilder.query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据任务号查询数据
     *
     * @param taskNo 任务号
     */
    public List<BuildPckScanTable> getDataByTaskNo(String taskNo) {
        try {
            QueryBuilder<BuildPckScanTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("taskNo", taskNo);
            return queryBuilder.query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
